{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# СЛОЖНЫЙ ФОРМАТ И ИХ ВСЕГО 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import csv\n",
    "import wfdb\n",
    "import heartpy as hp\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from scipy.signal import resample\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "age_groups = {15:1,16:1,17:1,18:1,19:1,\n",
    "             20:2,21:2,22:2,23:2,24:2,\n",
    "             25:3,26:3,27:3,28:3,29:3,\n",
    "             30:4,31:4,32:4,33:4,34:4,\n",
    "             35:5,36:5,37:5,38:5,39:5,\n",
    "             40:6,41:6,42:6,43:6,44:6,\n",
    "             45:7,46:7,47:7,48:7,49:7,\n",
    "             50:8,51:8,52:8,53:8,54:8,\n",
    "             55:9,56:9,57:9,58:9,59:9,\n",
    "             60:10,61:10,62:10,63:10,64:10,\n",
    "             65:11,66:11,67:11,68:11,69:11,\n",
    "             70:12,71:12,72:12,73:12,74:12,\n",
    "             75:13,76:13,77:13,78:13,79:13,\n",
    "             80:14,81:14,82:14,83:14,84:14,\n",
    "             85:15,86:15,87:15,88:15,89:15,\n",
    "             90:16,91:16,92:16,93:16,94:16,\n",
    "             95:17,96:17,97:17,98:17,99:17,100:17,\n",
    "             }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "dict_keys(['rpeaks', 'signal', 'label', 'activity', 'questionnaire', 'subject'])\n",
      "Shape of rest_bvp_signal_resampled: (300000, 1)\n",
      "None\n",
      "Error processing signal: {'id': 1, 'age_group': None, 'error': 'None'}\n"
     ]
    }
   ],
   "source": [
    "with open(\"D:\\\\Proga\\\\AML\\\\datasets\\\\ppg-dalia\\\\data\\\\PPG_FieldStudy\\\\S1\\\\S1.pkl\",\"rb\") as f:\n",
    "    data = pickle.load(f,encoding='latin1')\n",
    "\n",
    "print(type(data))\n",
    "print(data.keys())\n",
    "# print(data['signal'][\"wrist\"][\"BVP\"])\n",
    "# print(len(data['signal'][\"wrist\"][\"BVP\"]))\n",
    "# print(data['label'])\n",
    "# print(data['activity'])\n",
    "# print(data['activity'])\n",
    "\n",
    "# Получаем метки активности\n",
    "activity_labels = data['activity']\n",
    "\n",
    "# Получаем PPG-сигнал (BVP)\n",
    "bvp_signal = data['signal']['wrist']['BVP']\n",
    "\n",
    "# Получаем частоту сэмплирования PPG-сигнала\n",
    "bvp_sampling_rate = 64  # Гц\n",
    "\n",
    "# Определяем периоды времени, когда участник сидел (activity == 1)\n",
    "rest_periods = np.where(activity_labels == 1)[0]\n",
    "\n",
    "# Преобразуем индексы периодов покоя в диапазоны времени\n",
    "rest_start_times = rest_periods[0::2]\n",
    "rest_end_times = rest_periods[1::2]\n",
    "\n",
    "# Извлекаем PPG-сигналы для периодов покоя\n",
    "rest_bvp_signals = []\n",
    "for start, end in zip(rest_start_times, rest_end_times):\n",
    "    # Преобразуем индексы в временные метки\n",
    "    start_time = start / 4  # Частота сэмплирования activity_labels - 4 Гц\n",
    "    end_time = end / 4  # Частота сэмплирования activity_labels - 4 Гц\n",
    "    \n",
    "    # Преобразуем временные метки в индексы для PPG-сигнала\n",
    "    start_index = int(start_time * bvp_sampling_rate)\n",
    "    end_index = int(end_time * bvp_sampling_rate)\n",
    "    \n",
    "    # Извлекаем PPG-сигнал для периода покоя\n",
    "    rest_bvp_signals.append(bvp_signal[start_index:end_index])\n",
    "\n",
    "# Объединяем все PPG-сигналы в один массив\n",
    "rest_bvp_signal = np.concatenate(rest_bvp_signals)\n",
    "\n",
    "# Целевая частота дискретизации\n",
    "target_fs = 1000\n",
    "\n",
    "# Пересэмплирование PPG-сигнала до целевой частоты\n",
    "num_samples = int(len(rest_bvp_signal) * target_fs / bvp_sampling_rate)\n",
    "rest_bvp_signal_resampled = resample(rest_bvp_signal, num_samples).astype(np.float32)\n",
    "\n",
    "# Обрезка сигнала до первых 5 минут (300 секунд)\n",
    "five_minute_samples = 300 * target_fs\n",
    "rest_bvp_signal_resampled = rest_bvp_signal_resampled[:five_minute_samples]\n",
    "\n",
    "# Проверяем форму сигнала\n",
    "print(\"Shape of rest_bvp_signal_resampled:\", rest_bvp_signal_resampled.shape)\n",
    "\n",
    "# Обработка сигнала с использованием heartpy\n",
    "try:\n",
    "    wd, m = hp.process(rest_bvp_signal_resampled.flatten(), target_fs)\n",
    "    \n",
    "    # Получаем анкетные данные\n",
    "    questionnaire_data = data['questionnaire']\n",
    "    age = questionnaire_data.get('age', None)\n",
    "    print(age)\n",
    "    # Определяем возрастную группу\n",
    "    age_group = age_groups[age]\n",
    "    \n",
    "    # Создаем метаданные\n",
    "    result_metadata = {\n",
    "        'id': 1,  # Идентификатор записи\n",
    "        'age_group': age_group,\n",
    "        **m  # Метрики из heartpy\n",
    "    }\n",
    "    \n",
    "    # Преобразуем метаданные в список словарей\n",
    "    result_metadata_list = [result_metadata]\n",
    "\n",
    "except hp.exceptions.BadSignalWarning as e:\n",
    "    result_metadata = {\n",
    "        'id': 1,\n",
    "        'age_group': None,\n",
    "        'error': \"BadSignalWarning\"\n",
    "    }\n",
    "    print(\"Error processing signal:\", result_metadata)\n",
    "\n",
    "except Exception as e:\n",
    "    result_metadata = {\n",
    "        'id': 1,\n",
    "        'age_group': None,\n",
    "        'error': str(e)\n",
    "    }\n",
    "    print(\"Error processing signal:\", result_metadata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>age_group</th>\n",
       "      <th>bpm</th>\n",
       "      <th>ibi</th>\n",
       "      <th>sdnn</th>\n",
       "      <th>sdsd</th>\n",
       "      <th>rmssd</th>\n",
       "      <th>pnn20</th>\n",
       "      <th>pnn50</th>\n",
       "      <th>hr_mad</th>\n",
       "      <th>sd1</th>\n",
       "      <th>sd2</th>\n",
       "      <th>s</th>\n",
       "      <th>sd1/sd2</th>\n",
       "      <th>breathingrate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>None</td>\n",
       "      <td>118.893659</td>\n",
       "      <td>504.652651</td>\n",
       "      <td>177.246809</td>\n",
       "      <td>126.770589</td>\n",
       "      <td>208.349559</td>\n",
       "      <td>0.916342</td>\n",
       "      <td>0.735409</td>\n",
       "      <td>147.0</td>\n",
       "      <td>147.311601</td>\n",
       "      <td>200.610141</td>\n",
       "      <td>92840.977347</td>\n",
       "      <td>0.734318</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id age_group         bpm         ibi        sdnn        sdsd       rmssd  \\\n",
       "0   1      None  118.893659  504.652651  177.246809  126.770589  208.349559   \n",
       "\n",
       "      pnn20     pnn50  hr_mad         sd1         sd2             s   sd1/sd2  \\\n",
       "0  0.916342  0.735409   147.0  147.311601  200.610141  92840.977347  0.734318   \n",
       "\n",
       "   breathingrate  \n",
       "0            0.2  "
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_metadata = pd.DataFrame(result_metadata_list)\n",
    "#df_metadata.dropna(inplace=True)\n",
    "df_metadata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Per-column arrays must each be 1-dimensional",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[21], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m df_signals \u001b[38;5;241m=\u001b[39m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mDataFrame\u001b[49m\u001b[43m(\u001b[49m\u001b[43mresult_signals\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m      2\u001b[0m df_signals\n",
      "File \u001b[1;32mc:\\Users\\toha2\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\pandas\\core\\frame.py:778\u001b[0m, in \u001b[0;36mDataFrame.__init__\u001b[1;34m(self, data, index, columns, dtype, copy)\u001b[0m\n\u001b[0;32m    772\u001b[0m     mgr \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_init_mgr(\n\u001b[0;32m    773\u001b[0m         data, axes\u001b[38;5;241m=\u001b[39m{\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mindex\u001b[39m\u001b[38;5;124m\"\u001b[39m: index, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcolumns\u001b[39m\u001b[38;5;124m\"\u001b[39m: columns}, dtype\u001b[38;5;241m=\u001b[39mdtype, copy\u001b[38;5;241m=\u001b[39mcopy\n\u001b[0;32m    774\u001b[0m     )\n\u001b[0;32m    776\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(data, \u001b[38;5;28mdict\u001b[39m):\n\u001b[0;32m    777\u001b[0m     \u001b[38;5;66;03m# GH#38939 de facto copy defaults to False only in non-dict cases\u001b[39;00m\n\u001b[1;32m--> 778\u001b[0m     mgr \u001b[38;5;241m=\u001b[39m \u001b[43mdict_to_mgr\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindex\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcolumns\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcopy\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcopy\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtyp\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmanager\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    779\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(data, ma\u001b[38;5;241m.\u001b[39mMaskedArray):\n\u001b[0;32m    780\u001b[0m     \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mnumpy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mma\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m mrecords\n",
      "File \u001b[1;32mc:\\Users\\toha2\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\pandas\\core\\internals\\construction.py:503\u001b[0m, in \u001b[0;36mdict_to_mgr\u001b[1;34m(data, index, columns, dtype, typ, copy)\u001b[0m\n\u001b[0;32m    499\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    500\u001b[0m         \u001b[38;5;66;03m# dtype check to exclude e.g. range objects, scalars\u001b[39;00m\n\u001b[0;32m    501\u001b[0m         arrays \u001b[38;5;241m=\u001b[39m [x\u001b[38;5;241m.\u001b[39mcopy() \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(x, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdtype\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01melse\u001b[39;00m x \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m arrays]\n\u001b[1;32m--> 503\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43marrays_to_mgr\u001b[49m\u001b[43m(\u001b[49m\u001b[43marrays\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcolumns\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindex\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtyp\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtyp\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconsolidate\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcopy\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mc:\\Users\\toha2\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\pandas\\core\\internals\\construction.py:114\u001b[0m, in \u001b[0;36marrays_to_mgr\u001b[1;34m(arrays, columns, index, dtype, verify_integrity, typ, consolidate)\u001b[0m\n\u001b[0;32m    111\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m verify_integrity:\n\u001b[0;32m    112\u001b[0m     \u001b[38;5;66;03m# figure out the index, if necessary\u001b[39;00m\n\u001b[0;32m    113\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m index \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m--> 114\u001b[0m         index \u001b[38;5;241m=\u001b[39m \u001b[43m_extract_index\u001b[49m\u001b[43m(\u001b[49m\u001b[43marrays\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    115\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    116\u001b[0m         index \u001b[38;5;241m=\u001b[39m ensure_index(index)\n",
      "File \u001b[1;32mc:\\Users\\toha2\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\pandas\\core\\internals\\construction.py:664\u001b[0m, in \u001b[0;36m_extract_index\u001b[1;34m(data)\u001b[0m\n\u001b[0;32m    662\u001b[0m         raw_lengths\u001b[38;5;241m.\u001b[39mappend(\u001b[38;5;28mlen\u001b[39m(val))\n\u001b[0;32m    663\u001b[0m     \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(val, np\u001b[38;5;241m.\u001b[39mndarray) \u001b[38;5;129;01mand\u001b[39;00m val\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m--> 664\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPer-column arrays must each be 1-dimensional\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    666\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m indexes \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m raw_lengths:\n\u001b[0;32m    667\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIf using all scalar values, you must pass an index\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mValueError\u001b[0m: Per-column arrays must each be 1-dimensional"
     ]
    }
   ],
   "source": [
    "df_signals = pd.DataFrame(result_signals)\n",
    "df_signals"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
